<template>
  <div>
    <div><router-link to="/">首页</router-link></div>
    <div id="chart_example">
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'echarts3',
  data () {
    return {
    }
  },
  mounted () {
    // let this_ = this
    let myChart = echarts.init(document.getElementById('chart_example'))
    // 上面这一句是通用的，别删！！！！！！！

    // 我是华丽丽的的分割线，下面是散点图四
    var symbolSize = 20
    // var data = [[15, 0], [-50, 10], [-56.5, 20], [-46.5, 30], [-22.1, 40]]
    var data = []
    // var points = []

    let option = {
      title: {
        text: 'Click to Add Points(描点画图)'
      },
      tooltip: {
        formatter: function (params) {
          var data = params.data || [0, 0]
          return data[0].toFixed(2) + ', ' + data[1].toFixed(2)
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        min: -60,
        max: 20,
        type: 'value',
        axisLine: {onZero: false}
      },
      yAxis: {
        min: 0,
        max: 40,
        type: 'value',
        axisLine: {onZero: false}
      },
      series: [
        {
          id: 'a',
          type: 'line',
          smooth: true,
          symbolSize: symbolSize,
          data: data
        }
      ]
    }

    var zr = myChart.getZr()

    zr.on('click', function (params) {
      var pointInPixel = [params.offsetX, params.offsetY]
      var pointInGrid = myChart.convertFromPixel('grid', pointInPixel)

      if (myChart.containPixel('grid', pointInPixel)) {
        data.push(pointInGrid)

        myChart.setOption({
          series: [{
            id: 'a',
            data: data
          }]
        })
      }
    })

    zr.on('mousemove', function (params) {
      var pointInPixel = [params.offsetX, params.offsetY]
      zr.setCursorStyle(myChart.containPixel('grid', pointInPixel) ? 'copy' : 'default')
    })
    //

    // 我是华丽丽的分割线，下面的代码是通用的，别删！！！！！！！！
    myChart.setOption(option)
    // 建议加上以下这一行代码，不加的效果图如下（当浏览器窗口缩小的时候）。超过了div的界限（红色边框）
    window.addEventListener('resize', function () { myChart.resize() })
  },
  methods: {},
  watch: {},
  created () {
  }
}
</script>

<style scoped>
  h2{
    text-align: center;
    padding: 30px;
    font-size: 18px;
  }
  #chart_example{
    width: 50%;
    height: 500px;
    border: 1px solid red;
    margin: 0 auto;
  }
</style>
